package accountManagementNew.controller;


import accountManagementNew.common.base.PersonalQueryEntity;
import accountManagementNew.common.base.Result;
import accountManagementNew.common.enums.ResultCodeEnum;
import accountManagementNew.common.pojo.ConsumptionRecordPersonal;
import accountManagementNew.common.vo.BlurDataVO;
import accountManagementNew.common.vo.ConsumptionRecordPersonalVO;
import accountManagementNew.service.IConsumptionRecordPersonalService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.PushBuilder;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人消费记录控制层
 *
 * @author lihonbin
 * @since 2025-09-28
 */
@AllArgsConstructor
@RestController
@RequestMapping("/consumptionRecordPersonal")
public class ConsumptionRecordPersonalController {


    private IConsumptionRecordPersonalService consumptionRecordPersonalService;

    /**
     * 分页查询个人消费记录
     *
     * @param categoryId 分类id
     * @param userId     用户id
     * @param pageNum    页码
     * @param pageSize   页大小
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<ConsumptionRecordPersonal>> page(@RequestParam String categoryId,
                                                        @RequestParam String userId,
                                                        @RequestParam Integer pageNum,
                                                        @RequestParam Integer pageSize) {
        // 校验
        if (categoryId == null || userId == null) {
            return Result.error(ResultCodeEnum.METHOD_NOT_ALLOWED, "查询失败，请稍后重试！");
        }
        Page<ConsumptionRecordPersonal> page = new Page<>(pageNum, pageSize);
        PersonalQueryEntity personalQuery = new PersonalQueryEntity(categoryId, userId);
        Page<ConsumptionRecordPersonal> result = consumptionRecordPersonalService.queryPersonalConsumptionRecordByUsernameAndCategoryId(page, personalQuery);
        return Result.success(result);
    }

    /**
     * 查询相关消费类型的个人全部消费记录
     *
     * @param categoryId 分类id
     * @param userId     用户id
     * @return 消费记录列表
     */
    @GetMapping("/all")
    public Result<List<ConsumptionRecordPersonal>> list(@RequestParam String categoryId,
                                                        @RequestParam String userId) {
        // 校验
        if (categoryId == null || userId == null) {
            return Result.error(ResultCodeEnum.METHOD_NOT_ALLOWED, "查询失败，请稍后重试！");
        }
        PersonalQueryEntity personalQuery = new PersonalQueryEntity(categoryId, userId);
        List<ConsumptionRecordPersonal> result = consumptionRecordPersonalService.queryAllByCategoryIdAndUserId(personalQuery);
        return Result.success(result);
    }

    /**
     * 添加个人消费记录
     *
     * @param consumptionRecordPersonalVO 消费记录
     * @return 添加结果
     */
    @PostMapping("/append")
    public Result<String> appendedRecord(@RequestBody ConsumptionRecordPersonalVO consumptionRecordPersonalVO) {
        // 校验
        if (consumptionRecordPersonalVO == null || consumptionRecordPersonalVO.getUserId() == null) {
            return Result.error(ResultCodeEnum.FAIL, "添加失败，请稍后重试！");
        }
        ConsumptionRecordPersonal consumptionRecordPersonal = new ConsumptionRecordPersonal();
        consumptionRecordPersonal.setTypeId(consumptionRecordPersonalVO.getTypeId());
        consumptionRecordPersonal.setUserId(consumptionRecordPersonalVO.getUserId());
        consumptionRecordPersonal.setConsumptionDate(consumptionRecordPersonalVO.getConsumptionDate());
        consumptionRecordPersonal.setPaymentMethod(consumptionRecordPersonalVO.getPaymentMethod());
        consumptionRecordPersonal.setDescription(consumptionRecordPersonalVO.getDescription());
        consumptionRecordPersonal.setIsNecessary(consumptionRecordPersonalVO.getIsNecessary());
        consumptionRecordPersonal.setIsStatistics(consumptionRecordPersonalVO.getIsStatistics());
        consumptionRecordPersonal.setAmount(consumptionRecordPersonalVO.getAmount());
        boolean result = consumptionRecordPersonalService.save(consumptionRecordPersonal);
        if (result) {
            return Result.success("添加成功！");
        }
        return Result.error(ResultCodeEnum.FAIL, "添加失败，请稍后重试！");
    }

    /**
     * 删除指定的个人消费记录
     *
     * @param id 记录id
     * @return 删除结果
     */
    @DeleteMapping("/remove")
    public Result<String> removeRecord(@RequestParam String id) {
        if (id == null) {
            return Result.error(ResultCodeEnum.FAIL, "删除失败，请稍后重试！");
        }
        boolean result = consumptionRecordPersonalService.deletById(id);
        if (result) {
            return Result.success("删除成功！");
        }
        return Result.error(ResultCodeEnum.FAIL, "删除失败，请稍后重试！");
    }

    @PutMapping("/update")
    public Result<String> updateRecord(@RequestBody ConsumptionRecordPersonal consumptionRecordPersonal) {
        if (consumptionRecordPersonal == null){
            return Result.error(ResultCodeEnum.METHOD_NOT_ALLOWED, "请确保输入的信息正确");
        }
        boolean result = consumptionRecordPersonalService.modifyRecord(consumptionRecordPersonal);
        if (result) {
            return Result.success("修改成功");
        }else{
            return Result.error(ResultCodeEnum.FAIL, "修改失败,请重试！");
        }
    }


    /**
     * 模糊查询所有消费类型的模糊数据
     * @return 模糊数据
     */
    @GetMapping("/queryAllBlurData")
    public Result<List<BlurDataVO>> queryAllBlurData(@RequestParam String id) {
        return Result.success(consumptionRecordPersonalService.queryAllBlurDataByUserId(id));
    }

}
